package com.ten.shou.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.TbLink;
import com.ten.common.core.vo.Result;
import com.ten.shou.dao.LinkDao;
import com.ten.shou.service.LinkService;
import com.ten.shou.vo.LinkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/9 21:11
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class LinkServiceImpl implements LinkService {
    @Resource
    private LinkDao linkDao;

    @Override
    public Result<TbLink> queryAllLink(Integer current, Integer pageSize, LinkVo linkVo) {
        IPage<TbLink> page=new Page<>(current,pageSize);
        QueryWrapper<TbLink> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(linkVo.getLinkName())){
            wrapper.like("link_name",linkVo.getLinkName());
        }
        if (StringUtils.hasText(linkVo.getIsDisable())){
            wrapper.eq("is_disable",linkVo.getIsDisable());
        }
        IPage<TbLink> page1 = linkDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",page1);
    }

    @Override
    public Result<TbLink> insertAllLink(TbLink tbLink) {
        tbLink.setIsDisable(0);
        linkDao.insert(tbLink);
        return new Result<>(2000,"添加成功");
    }

    @Override
    public Result<TbLink> updateLinkStatus(String linkId ,String isDisable) {
        UpdateWrapper<TbLink> wrapper=new UpdateWrapper<>();
        if (StringUtils.hasText(linkId)){
            wrapper.eq("link_id",linkId);
        }
        if (StringUtils.hasText(isDisable)){
            wrapper.set("is_disable",isDisable);
        }
        linkDao.update(null,wrapper);
        return new Result(2000,"修改状态成功");
    }

    @Override
    public Result<TbLink> deleteLinkByLinkId(LinkVo linkVo) {
        linkDao.deleteById(linkVo.getLinkId());
        return new Result<>(2000,"删除成功");
    }

    @Override
    public Result<TbLink> updateLinkName(TbLink tbLink) {
        linkDao.updateById(tbLink);
        return new Result<>(2000,"修改成功");
    }

    @Override
    public Result getQianLink() {
        QueryWrapper<TbLink> wrapper=new QueryWrapper<>();
        wrapper.eq("is_disable","0");
        List<TbLink> tbLinks = linkDao.selectList(wrapper);
        return new Result(2000,"",tbLinks);
    }
}
